package cn.keniykey.mc.landterritory.landview;

import java.util.HashMap;
import java.util.regex.Pattern;

import javax.annotation.Nullable;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import cn.keniykey.mc.generallibrary.input.api.EInputResult;
import cn.keniykey.mc.generallibrary.input.api.IInputRequestManager;
import cn.keniykey.mc.generallibrary.input.api.IInputResultHandler;
import cn.keniykey.mc.landterritory.LandTerritoryMain;
import cn.keniykey.mc.landterritory.config.ConfigurationManager;
import cn.keniykey.mc.landterritory.config.LanguageManager;
import cn.keniykey.mc.landterritory.config.ViewsConfiguration;
import cn.keniykey.mc.landterritory.config.ViewsConfiguration.ViewConfig;
import cn.keniykey.mc.landterritory.event.LandCreateEvent;
import cn.keniykey.mc.landterritory.event.LandRangeUpdateEvent;
import cn.keniykey.mc.landterritory.land.LandManager;
import cn.keniykey.mc.landterritory.land.LandRangeToolListener;
import cn.keniykey.mc.landterritory.land.RangeShowManager;
import cn.keniykey.mc.landterritory.land.api.ELandType;
import cn.keniykey.mc.landterritory.land.api.Land;
import cn.keniykey.mc.landterritory.land.api.LandPoint;
import cn.keniykey.mc.landterritory.land.api.LandPosition;
import cn.keniykey.mc.landterritory.land.api.LandRange;
import cn.keniykey.mc.landterritory.land.api.LandUtil;
import cn.keniykey.mc.landterritory.land.api.LandUtil.ELandPermissionLevel;
import cn.keniykey.mc.landterritory.toolkit.ArithmeticUtil;
import cn.keniykey.mc.landterritory.toolkit.Util;
import cn.keniykey.mc.landterritory.vault.Econ;
import cn.keniykey.mc.landterritory.vault.Perm;
import cn.keniykey.mc.viewlibrary.container.api.EContainerType;
import cn.keniykey.mc.viewlibrary.view.api.AbFrameView;
import cn.keniykey.mc.viewlibrary.view.api.FrameViewBundle;
import cn.keniykey.mc.viewlibrary.view.api.FrameViewIntent;
import cn.keniykey.mc.viewlibrary.view.api.IViewClickHandler;
import cn.keniykey.mc.viewlibrary.view.api.ViewClickReport;
import cn.keniykey.mc.viewlibrary.view.api.template.AbChooseView;
import cn.keniykey.mc.viewlibrary.view.api.template.AbChooseView.EChooseResult;
import cn.keniykey.mc.viewlibrary.view.api.template.ChooseViewIntentBuilder;

public final class LandCreateView extends AbLandFrameView{

	protected LandCreateView(Player player1) {
		super(player1);
	}

	private static final String VIEW_NAME = "landcreate";
	
	private static final int CODE_CHOOSE_EXPAND_SKY = 10400;
	private static final int CODE_CHOOSE_EXPAND_GROUP = 10401;
	private static final int CODE_CHOOSE_LANDTOOL = 10402;
	private static final int CODE_CHOOSE_MIGRATE = 10403;
	
	@Override
	protected void onCreate() {
		super.onCreate();
		final ViewConfig config_view = ViewsConfiguration.getViewConfig(VIEW_NAME);
		this.setViewType(EContainerType.matchContainer(config_view.getViewSize()));
		this.setTitle(Util.conver(config_view.getViewTitle()));
		
		this.setIconClick(config_view.getViewItemSlot("back"), config_view.getViewItem("back"), AbLandFrameView.CLICK_BACK);
		this.setIconClick(config_view.getViewItemSlot("range_create"), config_view.getViewItem("range_create"), new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandCreateView.this.isMatchClickSpeed()) return;
				view.showView(new FrameViewIntent(LandRangeCreateView.class));
			}
		});
		this.setIconClick(config_view.getViewItemSlot("expand_sky"), config_view.getViewItem("expand_sky"), new IViewClickHandler() {	
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandCreateView.this.isMatchClickSpeed()) return;
				final ChooseViewIntentBuilder builder = new ChooseViewIntentBuilder(LandChooseView.class);
				builder.setChooseCountdown(ConfigurationManager.View_Choose_Countdown);
				builder.setCustomPromptName(LanguageManager.getLang(1104163));
				LandCreateView.this.showViewForResult(builder.build(), CODE_CHOOSE_EXPAND_SKY);
			}
		});
		this.setIconClick(config_view.getViewItemSlot("expand_group"), config_view.getViewItem("expand_group"), new IViewClickHandler() {	
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandCreateView.this.isMatchClickSpeed()) return;
				final ChooseViewIntentBuilder builder = new ChooseViewIntentBuilder(LandChooseView.class);
				builder.setChooseCountdown(ConfigurationManager.View_Choose_Countdown);
				builder.setCustomPromptName(LanguageManager.getLang(1104164));
				LandCreateView.this.showViewForResult(builder.build(), CODE_CHOOSE_EXPAND_GROUP);
			}
		});
		ItemStack item = ViewsConfiguration.setItemMeta(config_view.getViewItem("landtool"), null, Util.listFromStr(LanguageManager.getLang(1104000 ,
				ConfigurationManager.View_LandCreate_Tool_Price)));
		this.setIconClick(config_view.getViewItemSlot("landtool"), item, new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandCreateView.this.isMatchClickSpeed()) return;
				final ChooseViewIntentBuilder builder = new ChooseViewIntentBuilder(LandChooseView.class);
				builder.setChooseCountdown(ConfigurationManager.View_Choose_Countdown);
				builder.setCustomPromptName(LanguageManager.getLang(1104171));
				final int price = ConfigurationManager.View_LandCreate_Tool_Price;
				builder.setCustomPromptLore(Util.listFromStr(LanguageManager.getLang(1104172, price)));
				builder.setForwardContent(price);
				LandCreateView.this.showViewForResult(builder.build(), CODE_CHOOSE_LANDTOOL);
			}
		});
		this.setIconClick(config_view.getViewItemSlot("show"), config_view.getViewItem("show"), new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandCreateView.this.isMatchClickSpeed()) return;
				final LandRange range = LandRangeToolListener.getLandRange(player);
				if(range == null || !range.isRangeAllow()) return;
				RangeShowManager.showRange(player, range.clone(), true);
				view.finishAll();
			}
		});
		item = ViewsConfiguration.setItemMeta(config_view.getViewItem("migrate"), null,  Util.listFromStr(LanguageManager.getLang(1104004, ConfigurationManager.View_LandCreate_Migrate_Price)));
		this.setIconClick(config_view.getViewItemSlot("migrate"), item, new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandCreateView.this.isMatchClickSpeed()) return;
				final LandRange range = LandRangeToolListener.getLandRange(player);
				if(range == null) return;
				IInputRequestManager.getIInputRequestManager().requestInput(player, "s", ConfigurationManager.View_Choose_Countdown,true, new IInputResultHandler() {
					private final LandRange range1 = range;
					@Override
					public boolean onInputResult(EInputResult result, OfflinePlayer offplayer, @Nullable String str) {
						boolean cancel = true;
						if(EInputResult.INPUT.equals(result)) {
							cancel = LandCreateView.this.inputMigrateLand(str, this.range1);
						}
						LandViewUtil.inputResultResponse(result, offplayer, cancel, 1104200);
						return cancel;
					}
				});
				player.sendMessage(LanguageManager.getChatLang(1104201, "s"));
				view.hidden();
			}
		});
		//创建领地
		boolean hasvip = Perm.getPerm().hasPermission(this.getPlayer().getWorld(), this.getPlayer(), ConfigurationManager.Territory_Create_VipPer);
		item = ViewsConfiguration.setItemMeta(config_view.getViewItem("territory"), null ,Util.listFromStr(LanguageManager.getLang(1104001, ConfigurationManager.Territory_Create_Price,
				(hasvip ? "§c§m" : "§e"), ConfigurationManager.Territory_Create_Cost, (hasvip ? "§e" : "§c§m"), ConfigurationManager.Territory_Create_CostVip)));
		final boolean hasvip1 = hasvip;
		this.setIconClick(config_view.getViewItemSlot("territory"), item, new IViewClickHandler() {
			private final boolean vip1 = hasvip1;
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandCreateView.this.isMatchClickSpeed()) return;
				if(!requestInputCreateLand(player, ELandType.TERRITORY, this.vip1)) return;
				view.finishAll();
			}
		});
		//创建小区
		hasvip = Perm.getPerm().hasPermission(this.getPlayer().getWorld(), this.getPlayer(), ConfigurationManager.Community_Create_VipPer);
		item = ViewsConfiguration.setItemMeta(config_view.getViewItem("community"), null, Util.listFromStr(LanguageManager.getLang(1104002, ConfigurationManager.Community_Create_Price,
				(hasvip ? "§c§m" : "§e"), ConfigurationManager.Community_Create_Cost, (hasvip ? "§e" : "§c§m"), ConfigurationManager.Community_Create_CostVip)));
		final boolean hasvip2 = hasvip;
		this.setIconClick(config_view.getViewItemSlot("community"), item, new IViewClickHandler() {
			private final boolean vip1 = hasvip2;
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandCreateView.this.isMatchClickSpeed()) return;
				if(!requestInputCreateLand(player, ELandType.COMMUNITY, this.vip1)) return;
				view.finishAll();
			}
		});
		//创建地皮
		item = ViewsConfiguration.setItemMeta(config_view.getViewItem("ground"), null, Util.listFromStr(LanguageManager.getLang(1104003, ConfigurationManager.Ground_Create_Price)));
		this.setIconClick(config_view.getViewItemSlot("ground"), item, new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandCreateView.this.isMatchClickSpeed()) return;
				if(!requestInputCreateLand(player, ELandType.GROUND, false)) return;
				view.finishAll();
			}
		});
	}
	@Override
	protected void onShowBefore() {
		super.onShowBefore();
		this.refreshRangeInfo();
	}
	private void refreshRangeInfo() {
		final LandRange range = LandRangeToolListener.getLandRange(this.getPlayer());
		final ViewConfig config_view = ViewsConfiguration.getViewConfig(VIEW_NAME);
		final ItemStack item;
		if(range == null) {
			item = config_view.getViewItem("about");
		}else {
			final StringBuilder builder = new StringBuilder(LanguageManager.getLang(1006000));
			builder.append("\n").append(LandViewUtil.getLandRangeListStr(range));
			item = ViewsConfiguration.setItemMeta(config_view.getViewItem("about"), null, Util.listFromStr(builder.toString()));
		}
		this.setIcon(config_view.getViewItemSlot("about"), item);
	}
	
	@Override
	protected void onViewResult(int requestcode, int resultcode, @Nullable FrameViewBundle bundle) {
		super.onViewResult(requestcode, resultcode, bundle);
		switch(EChooseResult.muchResult(resultcode)) {
		case CONFIRM:
			if(CODE_CHOOSE_EXPAND_SKY == requestcode) {
				expandRange(this.getPlayer(), true);
				return;
			}
			if(CODE_CHOOSE_EXPAND_GROUP == requestcode) {
				expandRange(this.getPlayer(), false);
				return;
			}
			if(CODE_CHOOSE_LANDTOOL == requestcode) {
				buyLandTool(this.getPlayer(), bundle.getValue(AbChooseView.EChooseResultIntentKey.FORWARD.toString(), 0));
				break;
			}
			if(CODE_CHOOSE_MIGRATE == requestcode) {
				final HashMap<String,Object> map_values = bundle.getValue(AbChooseView.EChooseResultIntentKey.FORWARD.toString(),null);
				migrateLand(this.getPlayer(), (Land)map_values.get("land"), (LandRange)map_values.get("range_old"),
						(LandRange)map_values.get("range_new"), (double)map_values.get("pay"));
				map_values.clear();
				break;
			}
			this.getPlayer().sendMessage(LanguageManager.getChatLang(1001001) + requestcode);
			break;
		case VETO:
			return;
		default:
		}
		this.finishAll();
	}
	private static boolean requestInputCreateLand(Player player,ELandType type,boolean vip) {
		final LandRange range_select = LandRangeToolListener.getLandRange(player);
		if(range_select == null || !range_select.isRangeAllow()) return false;
		final String per;
		final int lang;
		final int price_service;
		final double price_cost;
		switch(type) {
		case TERRITORY:
			per = ConfigurationManager.Territory_Create_Per;
			lang = 1104300;
			price_service = ConfigurationManager.Territory_Create_Price;
			price_cost = (vip ? ConfigurationManager.Territory_Create_CostVip : ConfigurationManager.Community_Create_Cost);
			break;
		case COMMUNITY:
			per = ConfigurationManager.Community_Create_Per;
			lang = 1104301;
			price_service = ConfigurationManager.Community_Create_Price;
			price_cost = (vip ? ConfigurationManager.Community_Create_CostVip : ConfigurationManager.Community_Create_Cost);
			break;
		case GROUND:
			per = ConfigurationManager.Ground_Create_Per;
			lang = 1104302;
			price_service = ConfigurationManager.Ground_Create_Price;
			price_cost = 0d;
			break;
		default:
			per = null;
			lang = 0;
			price_service = 0;
			price_cost = 0d;
		}
		if(!LandViewUtil.isRangeAccordWithLand(range_select, type, player)) return false;
		if(!LandViewUtil.hasSendUsePermission(player, range_select.getWorldName(), per, lang)) return false;
		final LandRange range = range_select.clone();
		range.fit();
		final long size = range.getSize();
		final double price_range = (price_cost > 0d ? ArithmeticUtil.multiply(price_cost, size, 2) : 0d);
		final double price_pay = (price_range > 0d ? ArithmeticUtil.add(price_service, price_range) : price_service);
		final StringBuilder strb = new StringBuilder(LanguageManager.getLang(1104310));
		strb.append("\n").append(LandViewUtil.getLandRangeListStr(range_select));
		strb.append("\n").append(LanguageManager.getLang(1104311, price_cost, (vip ? "§l" : "§m"), size , price_range, price_service,
				price_pay));
		player.sendMessage(strb.toString());
		player.sendMessage(LanguageManager.getChatLang(1104312, "s", LanguageManager.getLang(type.getLang())));
		IInputRequestManager.getIInputRequestManager().requestInput(player, "s", ConfigurationManager.View_Choose_Countdown, true, new IInputResultHandler() {
			private final int lang1 = lang;
			private final LandRange range1 = range;
			private final double pay1 = price_pay;
			private final ELandType type1 = type;
			@Override
			public boolean onInputResult(EInputResult result, OfflinePlayer offplayer, @Nullable String str) {
				boolean cancel = true;
				if(EInputResult.INPUT.equals(result)) {
					cancel = inputCreateLand((Player)offplayer, this.range1, this.type1, this.pay1, str);
				}
				LandViewUtil.inputResultResponse(result, offplayer, cancel, this.lang1);
				return cancel;
			}
		});	
		return true;
	}
	private static boolean inputCreateLand(Player player,LandRange range,ELandType type,double price_pay,@Nullable String str) {
		if(!LandViewUtil.checkRangeCollision(player, type, range)) return true;
		if(str == null || Pattern.matches("^\\d+$", str) || Pattern.matches("\\s+", str)) {
			LandViewUtil.sendInputNotOnlyNumber(player);
			return false;
		}
		if(str.length() < 3 && str.length() > ConfigurationManager.View_LandInfo_Name_Length) {
			LandViewUtil.sendInputStrLength(player, 3, ConfigurationManager.View_LandInfo_Name_Length);
			return false;
		}
		if(LandManager.getLandManager().getLand(str) != null) {
			player.sendMessage(LanguageManager.getChatLang(1104313, str));
			return false;
		}
		if(!LandUtil.canHasNewLand(player.getUniqueId(), type)) {
			player.sendMessage(LanguageManager.getChatLang(1104320, str, LanguageManager.getLang(type.getLang())));
			return true;
		}
		final LandCreateEvent event = new LandCreateEvent(player, range, str);
		Bukkit.getPluginManager().callEvent(event);
		if(event.isCancelled()) {
			player.sendMessage(LanguageManager.getChatLang(1104316, str, LanguageManager.getLang(type.getLang())));
			return true;
		}
		if(price_pay > 0d && !player.isOp() && !Econ.getEcon().take(player, price_pay)) {
			player.sendMessage(LanguageManager.getChatLang(1104314, str, LanguageManager.getLang(type.getLang()), price_pay));
			return true;
		}
		final Location loc_player = player.getLocation();
		final boolean in_range = range.isInRange(loc_player);
		final LandPosition pos_land;
		if(in_range) {
			pos_land = new LandPosition(loc_player);
		}else {
			final LandPoint point = range.getCenterPoint();
			pos_land = new LandPosition(point.getWorldName(), (point.getX()+0.5d), (point.getY()+0.1d), (point.getZ()+0.5d), 0f, 0f);
		}
		final Land land = LandManager.getLandManager().createLand(player.getUniqueId(), str, type, range, pos_land);
		if(land == null) {
			player.sendMessage(LanguageManager.getChatLang(1104315, str, LanguageManager.getLang(type.getLang())));
			if(price_pay > 0d && !player.isOp() && !Econ.getEcon().give(player, price_pay)) {
				LandTerritoryMain.getPlugin().getLogger().warning(LanguageManager.getLang(1001002, player.getName(),
						player.getUniqueId().toString(),price_pay,"give"));
				player.sendMessage(LanguageManager.getChatLang(1001004, price_pay));
			}
			return true;
		}
		LandRangeToolListener.putLandRange(player, null);
		player.sendMessage(LanguageManager.getChatLang(1104317, price_pay, LandViewUtil.getLandLabelStr(land)));
		player.sendMessage(LanguageManager.getChatLang((in_range ? 1104318 : 1104319)));
		return true;
	}
	private boolean inputMigrateLand(@Nullable String str,LandRange range) {
		final Player player = this.getPlayer();
		if(str == null || str.isEmpty() || Pattern.matches("\\s+", str)) {
			player.sendMessage(LanguageManager.getChatLang(1104202));
			return false;
		}
		final Land land = (Pattern.matches("^\\d+$", str) ? LandManager.getLandManager().getLand(Long.valueOf(str)) :
			LandManager.getLandManager().getLand(str));
		if(land == null) {
			player.sendMessage(LanguageManager.getChatLang(1104203));
			return false;
		}
		final long size_old = land.getRangeSize();
		final long size_new = range.getSize();
		final long size_expand = Math.max(0l, (size_new - size_old));
		final double price_cost = getCreateCostPrice(land.getType(), range.getWorldName(), player);
		final double price_range = (size_expand > 0 && price_cost > 0d ? ArithmeticUtil.multiply(size_expand, price_cost, 2) : 0d);
		final double price_pay = (price_range > 0d ? ArithmeticUtil.add(ConfigurationManager.View_LandCreate_Migrate_Price, price_range) :
			ConfigurationManager.View_LandCreate_Migrate_Price);
		final ChooseViewIntentBuilder builder = new ChooseViewIntentBuilder(LandChooseView.class);
		builder.setChooseCountdown(ConfigurationManager.View_Choose_Countdown);
		builder.setCustomPromptName(LanguageManager.getLang(1104204));
		builder.setCustomPromptLore(Util.listFromStr(LanguageManager.getLang(1104205, size_old, size_new, price_cost,
				size_expand, price_range, ConfigurationManager.View_LandCreate_Migrate_Price, price_pay)));
		final HashMap<String,Object> map_values = new HashMap<>();
		map_values.put("pay", price_pay);
		map_values.put("land", land);
		map_values.put("range_old", land.getRange().clone());
		map_values.put("range_new", range);
		builder.setForwardContent(map_values);
		this.showViewForResult(builder.build(), CODE_CHOOSE_MIGRATE);
		return true;
	}
	private static void migrateLand(Player player,Land land,LandRange range_old, LandRange range_new,double price_pay) {
		if(land.isDeleted() || !land.getRange().equals(range_old)) {
			player.sendMessage(LanguageManager.getChatLang(1104206, LandViewUtil.getLandLabelStr(land)));
			return;
		}
		if(!LandViewUtil.hasSendUsePermission(player, range_new.getWorldName(), ConfigurationManager.View_LandCreate_Migrate_Per, 1104200)) return;
		if(!LandViewUtil.hasSendLandPermission(player, land, ELandPermissionLevel.OWNER, 1104200)) return;
		if(!LandViewUtil.checkRangeExpandCollision(player, land, range_new)) return;
		if(!LandViewUtil.checkRangeReduceCollision(player, land, range_new)) return;
		if(price_pay > 0d && !player.isOp() && !Econ.getEcon().take(player, price_pay)) {
			player.sendMessage(LanguageManager.getChatLang(1104207, price_pay));
			return;
		}
		final LandRangeUpdateEvent event = new LandRangeUpdateEvent(player, land, range_new);
		Bukkit.getPluginManager().callEvent(event);
		if(event.isCancelled()) {
			if(price_pay > 0d && !player.isOp() && !Econ.getEcon().give(player, price_pay)) {
				LandTerritoryMain.getPlugin().getLogger().warning(LanguageManager.getLang(1001002, player.getName(),
						player.getUniqueId().toString(),price_pay,"give"));
				player.sendMessage(LanguageManager.getChatLang(1001004, price_pay));
				return;
			}
			player.sendMessage(LanguageManager.getChatLang(1104208, LandViewUtil.getLandLabelStr(land)));
			return;
		}
		LandRangeToolListener.putLandRange(player, null);
		LandManager.getLandManager().removeLandInHash(range_old, land);
		land.setRange(range_new);
		LandManager.getLandManager().addLandInHash(range_new, land);
		LandViewUtil.checkSendLandPosInRange(player, land);
	}
	private static void buyLandTool(Player player,int price_pay) {
		if(!LandViewUtil.hasSendUsePermission(player, player.getWorld().getName(), ConfigurationManager.View_LandCreate_Tool_Per, 1104170)) return;
		final ItemStack item_main_hand = player.getInventory().getItemInMainHand();
		final boolean main_hand_empty = (item_main_hand == null || Material.AIR.equals(item_main_hand.getType()));
		if(!main_hand_empty && Util.getVacancyAmount(player.getInventory()) < 1) {
			player.sendMessage(LanguageManager.getChatLang(1104173));
			return;
		}
		if(price_pay > 0 && !player.isOp() && !Econ.getEcon().take(player, price_pay)) {
			player.sendMessage(LanguageManager.getChatLang(1104174, price_pay));
			return;
		}
		if(main_hand_empty) {
			player.getInventory().setItemInMainHand(LandRangeToolListener.getLandRangeTool());
		}else {
			player.getInventory().addItem(LandRangeToolListener.getLandRangeTool());
		}
		player.sendMessage(LanguageManager.getChatLang(1104175, price_pay));
		
	}
	private static void expandRange(Player player,boolean sky) {
		final LandRange range = LandRangeToolListener.getLandRange(player);
		if(range == null || !range.isRangeAllow()) {
			player.sendMessage(LanguageManager.getChatLang(1104165));
			return;
		}
		final World world = Bukkit.getWorld(range.getWorldName());
		if(world == null) {
			LanguageManager.getChatLang(1104160, range.getWorldName());
			return;
		}
		range.fit();
		if(sky) {
			final LandPoint point = range.getLandPointTwo();
			final int grid = world.getMaxHeight();
			point.setY(grid);
			player.sendMessage(LanguageManager.getChatLang(1104161, grid));
			return;
		}
		final LandPoint point = range.getLandPointOne();
		final int grid = LandViewUtil.getWorldMinHighly(world);
		point.setY(grid);
		player.sendMessage(LanguageManager.getChatLang(1104162, grid));
	}
	
	static double getCreateCostPrice(ELandType type, String worldname, Player player) {
		final String per;
		final double price_cost_vip,price_cost;
		switch(type) {
		case TERRITORY:
			per = ConfigurationManager.Territory_Create_VipPer;
			price_cost = ConfigurationManager.Territory_Create_Cost;
			price_cost_vip = ConfigurationManager.Community_Create_CostVip;
			break;
		case COMMUNITY:
			per = ConfigurationManager.Community_Create_VipPer;
			price_cost = ConfigurationManager.Community_Create_Cost;
			price_cost_vip = ConfigurationManager.Community_Create_CostVip;
			break;
		default:
			per = null;
			price_cost = (price_cost_vip = 0d);
		}
		if(per != null && !Perm.getPerm().hasPermission(worldname, player, per)) return price_cost;
		return price_cost_vip;
	}
	static int getCreateServicePrice(ELandType type) {
		switch(type) {
		case TERRITORY:
			return ConfigurationManager.Territory_Create_Price;
		case COMMUNITY:
			return ConfigurationManager.Community_Create_Price;
		case GROUND:
			return ConfigurationManager.Ground_Create_Price;
		default:
		}
		return 0;
	}
}